﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FileCopy
{
    public partial class f_DirForm : Form
    {
        private f_Directory destDirectory;
        private bool isModify = false;
        public f_DirForm(f_Directory dir)
        {
            InitializeComponent();
            this.isModify = true;
            if (dir != null)
                this.destDirectory = dir;
            else
                this.destDirectory = new f_Directory();
            checkBoxTimeValid.Checked = this.destDirectory.StartTime != this.destDirectory.EndTime;
            tStartTime.Enabled = this.destDirectory.StartTime != this.destDirectory.EndTime;
            tEndTime.Enabled = this.destDirectory.StartTime != this.destDirectory.EndTime;
        }

        private f_Item item;
        public f_DirForm(TreeNode node)
        {
            InitializeComponent();
            this.isModify = false;
            item = node.Tag as f_Item;
            this.destDirectory = new f_Directory();
            checkBoxTimeValid.Checked = this.destDirectory.StartTime != this.destDirectory.EndTime;
            tStartTime.Enabled = this.destDirectory.StartTime != this.destDirectory.EndTime;
            tEndTime.Enabled = this.destDirectory.StartTime != this.destDirectory.EndTime;
        }

        private void f_DirForm_Load(object sender, EventArgs e)
        {
            if (destDirectory != null)
            {
                tDestFileDir.Text = destDirectory.DestDirectory;
                checkBoxEnabled.Checked = destDirectory.Enabled;
                tStartTime.Text = destDirectory.StartTime;
                tEndTime.Text = destDirectory.EndTime;
            }
            ResetDir();
        }

        private void ResetDir()
        {
            if (destDirectory != null)
            {
                listBoxMain.Items.Clear();
                if (destDirectory.Filters != null)
                    listBoxMain.Items.AddRange(destDirectory.Filters);
            }
        }

        private void btnEditFilters_Click(object sender, EventArgs e)
        {
            using (f_EditFilterForm eff = new f_EditFilterForm(destDirectory))
            {
                if (eff.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    ResetDir();
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            destDirectory.DestDirectory = tDestFileDir.Text;
            destDirectory.Enabled = checkBoxEnabled.Checked;
            destDirectory.StartTime = tStartTime.Text;
            destDirectory.EndTime = tEndTime.Text;
            if (string.IsNullOrEmpty(destDirectory.DestDirectory))
            {
                MessageBox.Show("未正确设置路径...", "提示");
                return;
            }
            else if (!isModify)
            {
                item.AddDestPath(destDirectory);
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void checkBoxTimeValid_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxTimeValid.Checked)
            {
                tStartTime.Enabled = true;
                tEndTime.Enabled = true;
                tStartTime.Text = "08:00:00";
                tEndTime.Text = "20:00:00";
            }
            else
            {
                tStartTime.Enabled = false;
                tEndTime.Enabled = false;
                tStartTime.Text = "00:00:00";
                tEndTime.Text = "00:00:00";
            }
        }
    }
}
